import threading
import os
import time
from tkinter import Text,END,INSERT

class Loop_exists_file_thread(threading.Thread):
    def __init__(self,
                 status_bar: Text,
                 file_name: str,
                 Interval_time: int = 30):
        super().__init__()
        self.daemon = True
        self._stop_event = threading.Event()
        self.file_name = file_name
        self.interval_time = Interval_time
        self.status_bar = status_bar

    def run(self):
        exists_count = 0
        while not self._stop_event.is_set():
            exists_count = exists_count+1
            self._stop_event.wait(timeout=self.interval_time)
            if os.path.exists(self.file_name):
                print(f"{self.file_name}...下载完成")
                self.status_bar.insert(END,f"{self.file_name}...下载完成\n")
                self.status_bar.mark_set(INSERT, "end")
                self.status_bar.see("end")
                self._stop_event.set()
                break
            print(f"{self.file_name},下载中....{exists_count}")
            self.status_bar.insert(END,f"{self.file_name},下载中....{exists_count}\n")
            self.status_bar.mark_set(INSERT, "end")
            self.status_bar.see("end")

    def stop(self):
        self._stop_event.set()

    def is_running(self):
        return not self._stop_event.is_set()


if __name__ == '__main__':
    # t = Loop_exists_file_thread("c:/111.txt", 5)
    # t.daemon = True
    # t.start()
    # i = 0
    # while t.is_running():
    #     i = i+1
    #     print(f"running...{i}")
    #     time.sleep(2)
    ...
